package org.biubiu0002.baiblog.core.component;

import com.alibaba.fastjson.JSON;
import org.biubiu0002.baiblog.core.async.event.FollowEvent;
import org.biubiu0002.baiblog.core.async.event.PostShareEvent;
import org.biubiu0002.baiblog.core.async.event.UpdateShareEvent;
import org.biubiu0002.baiblog.core.async.event.UpdateUserStatusEvent;
import org.biubiu0002.baiblog.core.config.RabbitMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: biubiu0002
 * @Date: 2022-05-22 17:46
 **/
@Service
public class RabbitMqServiceImpl implements RabbitMqService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendUpdateUserStatusEvent(UpdateUserStatusEvent updateUserStatusEvent) {
        rabbitTemplate.convertAndSend(RabbitMqConfig.UPDATE_USER_STATUS_EXCHANGE,
                RabbitMqConfig.UPDATE_USER_STATUS_ROUTING_KEY,
                JSON.toJSONString(updateUserStatusEvent));
    }

    @Override
    public void sendPostShareEvent(PostShareEvent postShareEvent) {
        rabbitTemplate.convertAndSend(RabbitMqConfig.POST_SHARE_EXCHANGE,
                RabbitMqConfig.POST_SHARE_ROUTING_KEY,
                JSON.toJSONString(postShareEvent));
    }

    @Override
    public void sendUpdateShareEvent(UpdateShareEvent updateShareEvent) {
        rabbitTemplate.convertAndSend(RabbitMqConfig.UPDATE_SHARE_EXCHANGE,
                RabbitMqConfig.UPDATE_SHARE_ROUTING_KEY,
                JSON.toJSONString(updateShareEvent));
    }

    @Override
    public void sendFollowEvent(FollowEvent followEvent) {
        rabbitTemplate.convertAndSend(RabbitMqConfig.FOLLOW_EXCHANGE,
                RabbitMqConfig.FOLLOW_ROUTING_KEY,
                JSON.toJSONString(followEvent));
    }
}
